import java.util.Arrays;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-10-13
 * Time：19:30
 */
public class TestDemo {
    public static void main6(String[] args) {
        int[] arr = {1,2,3,4};
        int[] copy = Arrays.copyOf(arr, 2 * arr.length);
        System.out.println(Arrays.toString(copy));

        copy = new int[]{0,0,0,0,0};
        System.arraycopy(arr, 0, copy, 0, 4);
        System.out.println(Arrays.toString(copy));

        copy = arr.clone();
        System.out.println(Arrays.toString(copy));
        System.out.println(copy);
        System.out.println(arr);
    }
    /**
     * 写一个函数，将数组以字符串的形式输出[1, 2, 3, 4]
     * @param array
     * @return
     */
    public static String myToString(int[] array) {
        String ret = new String();
        ret += "[";
        for (int i = 0; i < array.length - 1; i++) {
            ret += Integer.toString(array[i]);
            ret += ", ";
        }
        ret += Integer.toString(array[array.length-1]);
        ret += "]";
        return ret;
    }
    public static void main(String[] args) {
        int[] array = {1,2,3,4};
        String ret = myToString(array);
        System.out.println(ret);
    }
    public static void fun1(int[] arr) {
        arr = new int[]{11,22,33,44};
    }
    public static void fun2(int[] arr) {
        arr[0] = 99;
    }
    public static int[] fun3() {
        int[] arr = {1,2,3,4};
        return arr;
    }
    public static void main4(String[] args) {
        int[] arr = {1,2,3};
        //存的是堆上地址的哈希值
        System.out.println(arr);

        fun1(arr);
        System.out.println(Arrays.toString(arr));
        fun2(arr);
        System.out.println(Arrays.toString(arr));

        int[] arr2 = fun3();
        System.out.println(Arrays.toString(arr2));
    }
    public static void main3(String[] args) {
        int[] arr = {1,2,3,4,5};
        //方式1
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        //方式2：增强for
        for ( int x: arr) {
            System.out.print(x + " ");
        }
        System.out.println();
        //方式3
        String ret = Arrays.toString(arr);
        System.out.println(ret);
    }
    public static void main2(String[] args) {
        int[] arr;
        //此时只能以下面这种方式初始化
        arr = new int[]{1,2,3,4,5};
        //修改值
        arr = new int[]{2,3,4,5,6};

        int[] arr2 = {1,2,3,4,5};
        //修改值
        arr2 = new int[]{6,7,8,9,10};
    }
    public static void main1(String[] args) {
        //初始化
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        int[] arr2 = new int[]{1,2,3,4,5,6,7,8,9,10};
        //未初始化，默认值为0或NULL或FALSE
        int[] arr3 = new int[10];
    }
}
